<?php

namespace App\Http\Controllers\Mini;

use App\Models\Users;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

use Illuminate\Support\Facades\Storage;
use App\User;
use Illuminate\Support\Facades\DB;

use App\Models\BalanceLogs;

class UserController extends Controller{
    
    public function getRank(Request $request){
        try{
        	$num = (int)$request->post('rank_num',3);
        	$page = (int)$request->post('page',1);
        	$list = [];
        	$date_start = date('Y-m-d H:i:s', strtotime(date('Y-m', time()) . '-01 00:00:00'));
        	$date_end = date('Y-m-d H:i:s', strtotime(date('Y-m', time()) . '-' . date('t', time()) . ' 00:00:00'));
       	    
        	$logs = DB::table('balance_logs as a')
        		    ->select( 
	        			array(
		        		
							\DB::raw('SUM(a.money) as total'),
	        					'b.nickname','b.avatar',"a.user_id"
	        			)
        		    )
        			->join('users as b',function($join) {
		        		$join->on('a.user_id', '=', 'b.id')->where("b.type","<>",3)->where("b.status","<>",4);
		        	})
		        	->where("a.type",1)
		        	->where("a.status",1)		        			        	
        			->whereBetween("a.created_at",array($date_start,$date_end))
        			->groupby('a.user_id')
		        	->orderby("total","DESC")->limit($num)->get();
          
            return response()->json([
                "code" => 1,
                "msg" => "获取成功",
                "data" => $logs
            ]);
        }catch (\Exception $e){
            return response()->json(["code"=>0, "msg"=>$e->getMessage()]);
        }
    }
    

}